package view.impl;

import dao.BusinessDao;
import dao.impl.BusinessDaoImpl;
import model.Business;
import view.BusinessView;

import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;

/**
 * @author 许中
 * @date 2024-08-20 09:29:49
 */
public class BusinessViewImpl implements BusinessView {

    private BusinessDao businessDao=new BusinessDaoImpl();

    @Override
    public void listBusinessAll() {
        List<Business> businesses = businessDao.listBusiness(null, null);
        if(businesses!=null&&businesses.size()>0){
            for (int i = 0; i < businesses.size(); i++) {
                System.out.println(businesses.get(i));
            }
        }else{
            System.out.println("商家列表为空");
        }

    }

    @Override
    public void listBusiness() {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入商户名称：");
        String businessName = sc.nextLine();
        System.out.println("请输入商户地址：");
        String businessAddress = sc.nextLine();
        List<Business> businesses =businessDao.listBusiness(businessName,businessAddress);
        if(businesses!=null&&businesses.size()>0){
            for (int i = 0; i < businesses.size(); i++) {
                System.out.println(businesses.get(i));
            }
        }else{
            System.out.println("商家列表为空");
        }
    }

    @Override
    public void saveBusiness() {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入商户名称：");
        String businessName = sc.nextLine();
        int result = businessDao.saveBusiness(businessName);
        if(result>0){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

    @Override
    public void removeBusiness() {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入商户id：");
        String businessId = sc.nextLine();
        int result = businessDao.removeBusiness(Integer.parseInt(businessId));
        if(result>0){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }

    @Override
    public Business login() {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入商户id：");
        Integer businessId = Integer.parseInt(sc.nextLine());
        System.out.println("请输入商户密码：");
        String password = sc.nextLine();
        Business result = businessDao.getBusinessByIdByPass(businessId, password);
        return result;
    }

    @Override
    public void showBusiness(Integer businessId) {
        Business business = businessDao.getBusinessById(businessId);
        if(business==null){
            System.out.println("未找到商户数据");
        }else{
            System.out.println(business);
        }
    }

    @Override
    public void editBusiness(Integer businessId) {
        Scanner sc=new Scanner(System.in);

        System.out.println("请输入新的商户名称：");
        String businessName = sc.nextLine();
        if(businessName==null||businessName.equals("")){
            businessName=null;
        }

        System.out.println("请输入新的商户地址：");
        String businessAddress = sc.nextLine();
        if(businessAddress==null||businessAddress.equals("")){
            businessAddress=null;
        }
        System.out.println("请输入新的商户简介：");
        String businessExplain = sc.nextLine();
        if(businessExplain==null||businessExplain.equals("")){
            businessExplain=null;
        }
        System.out.println("请输入新的商户起送价格：");
        String starPriceStr=sc.nextLine();
        BigDecimal starPrice = null;
        if(starPriceStr==null||starPriceStr.equals("")) {
            starPrice = null;
        }else{
            starPrice=new BigDecimal(starPriceStr);
        }

        System.out.println("请输入新的商户配送价格：");
        String deliveryPriceStr=sc.nextLine();
        BigDecimal deliveryPrice = null;
        if(deliveryPriceStr==null||deliveryPriceStr.equals("")) {
            deliveryPrice = null;
        }else{
            deliveryPrice=new BigDecimal(deliveryPriceStr);
        }


        Business business=new Business(businessId,null,businessName,businessAddress,businessExplain,starPrice,deliveryPrice);

        businessDao.updateBusiness(business);
    }

    @Override
    public void updateBusinessByPassword(Integer businessId) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入新的商户密码：");
        String password = sc.nextLine();
        businessDao.updateBusinessByPassword(businessId,password);
    }
}
